<template>
    <div>
        <!-- 商品分类 -->
    <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb> -->
    <!-- 卡片 -->
    <el-card>
            <el-button type="primary" @click="addcare">添加</el-button>
<tree-table
	:data="splist"
	:columns="defaultProps"
	:selection-type="false"
	:expand-type="false"
	index-text="索引"
	:show-row-hover="false"
	show-index
	border
>
	<!-- 是否有效 -->
	<template slot="isok" slot-scope="scope">
		<i class="el-icon-success" style="color: lightgreen" v-if="scope.row.cat_deleted === false"></i>
    </template>
	<!-- 排序 -->
	<template slot="sorts" slot-scope="scope">
		<el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
        <el-tag type="success" v-if="scope.row.cat_level === 1">二级</el-tag>
        <el-tag type="warning" v-if="scope.row.cat_level === 2">三级</el-tag>
	</template>
	<!-- 操作 -->
	<template slot="caozuo" slot-scope="scope">
        <el-button type="primary" size="mini" @click="editcate(scope.row.cat_id,scope.row.cat_name)">编辑</el-button>
        <el-button type="danger" size="mini" @click="del(scope.row.cat_id)">删除</el-button>
	</template>
</tree-table>
        <!-- 分页 -->
        <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage4"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="getCateParams.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
    <!-- 添加分类 -->
    <el-dialog title="添加分类" :visible.sync="dialogFormVisible">
  <el-form :model="addform">
    <el-form-item label="分类名称" :label-width="formLabelWidth">
      <el-input v-model="addform.cat_name" autocomplete="off"></el-input>
    </el-form-item>

    <el-form-item label="父级分类" :label-width="formLabelWidth">
     <el-cascader
     v-model="addform.cat_pid"
    :options="splist2"
    :props="addFormdefaultProps"
    clearable></el-cascader>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="dialogFormVisible = false">取 消</el-button>
    <el-button type="primary" @click="add">确 定</el-button>
  </div>
</el-dialog>
<!-- 修改 -->
<el-dialog title="修改分类" :visible.sync="dialogTableVisible">
    <el-form :model="addforms">
        <el-form-item label="分类名称" :label-width="formLabelWidth">
      <el-input v-model="addforms.cat_name" autocomplete="off"></el-input>
    </el-form-item>
    </el-form>
<div slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible = false">取 消</el-button>
        <el-button type="primary" @click="qd"
          >确 定</el-button>
      </div>
</el-dialog>
    </div>
</template>
<script>
    import {categories,addcategories,delcate,editcate} from '@/http/goods'
export default {
  data() {
    return {
      splist: [{}],
      defaultProps: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          // 表示当前列为模板列
          type: "template",
          // 表示当前这一列的模板名称
          template: "isok"
        },
        {
          label: "排序",
          type: "template",
          template: "sorts"
        },
        {
          label: "操作",
          type: "template",
          template: "caozuo"
        }
      ],
      total:0,
      currentPage4: 4,
      getCateParams: {
        pagenum: 1,
        pagesize: 5
      },
      splist2:[],
      dialogFormVisible:false,
      formLabelWidth: "120px",
      addform:{
        cat_name:'',
        cat_pid:'',
        cat_level: ''
      },
      addFormdefaultProps:{
        label: "cat_name",
        value: "cat_id",
        checkStrictly: true
      },
      dialogTableVisible:false,
      addforms:{
        cat_name:''
      },
      cateid:''
    };
  },
  methods: {
    //请求数据
    async getgoodsr(){
        let res =await categories(this.getCateParams)
        console.log(res);
        this.splist=res.data.result
        this.total=res.data.total
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.getCateParams.pagesize = val;
      this.getgoodsr()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.getCateParams.pagenum = val;
      this.getgoodsr()
    },
    // 添加按钮
   async addcare(){
    let res =await categories()
    this.splist2 =res.data
    this.dialogFormVisible=true
    },
    // 添加
  async  add(){
    let length =this.addform.cat_pid.length;
    let cat_pid =length === 0 ? 0 : this.addform.cat_pid[length - 1];
    let cat_level = length === 0 ? 0 : length;
    let res = await addcategories({
        cat_name: this.addform.cat_name,
        cat_pid: cat_pid,
        cat_level
    })
    this.getgoodsr()
      this.dialogFormVisible = false;
    },
    //删除
   async del(id){
        let res = await delcate(id)
        if(res){
            this.$message.success(res.meta.msg)
            this.getgoodsr()
        }
    },
    editcate(id,name){
        
        this.addforms.cat_name=name
        this.cateid=id
        this.dialogTableVisible=true
    },
   async qd(){
        let res =await editcate(this.cateid,this.addforms)
        if(res){
            this.$message.success(res.meta.msg)
            this.getgoodsr()
            this.dialogTableVisible=false
        }
    }
  },
  components: {},
  computed: {},
  watch: {},
  created() {
    this.getgoodsr()
  }
};
</script>
<style lang="scss" scoped>
</style>